package com.mes.basicdata.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IResourceAggregateDao;
import com.mes.basicdata.dao.iface.IWorkCenterDao;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.ResourceAggregate;
import com.mes.basicdata.domain.WorkCenter;
import com.mes.common.dao.BasicDao;

@Repository
public class WorkCenterDao extends BasicDao<WorkCenter> implements IWorkCenterDao{
	@Autowired
	private IResourceAggregateDao resourceAggregateDao;

	public WorkCenter getWorkCenterByDeviceUid(String deviceUid) throws Exception{
		List<ResourceAggregate> list = resourceAggregateDao.findByProperty("deviceUid", deviceUid);
		if (null!=list&&list.size()>0) {
		ResourceAggregate resourceAggregate = list.get(0);
		WorkCenter workCenter = resourceAggregate.getWorkCenter();
		return workCenter;
		}
		return null;
	}
	
	@Override
	public void getWorkCenterByConditions(Page<WorkCenter> page, Map<String, Object> params) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ WorkCenter.class.getName() + " a where 1=1 ");

		String workCenterName = (String) params.get("workCenterName");
		if (StringUtils.isNotEmpty(workCenterName)) {
			coreHql.append(" and a.workCenterName like :workCenterName ");
			args.put("workCenterName", "%" + workCenterName + "%");
		}
		String workCenterUid = (String) params.get("workCenterUid");
		if (StringUtils.isNotEmpty(workCenterUid)) {
			coreHql.append(" and a.workCenterUid like :workCenterUid ");
			args.put("workCenterUid", "%" + workCenterUid + "%");
		}
		
	/*	String parentOpUid = (String) params.get("parentOpUid");
		if (StringUtils.isNotEmpty(parentOpUid)) {
			coreHql.append(" and a.parentOpUid = parentOpUid ");
		}
		*/
		String countHql = "select count(*) " + coreHql.toString();
//			String hql = coreHql.toString() + "order by a.dept.id,a.id";
		String hql = coreHql.toString();
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
			
			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
		
	}
	

	@Override
	public void getWorkCentersByDept(Page<WorkCenter> page, Map<String, Object> params) {
		String queryString = "from WorkCenter a where a.deptUid = :deptUid";
		String countQueryString = "select count(*) from WorkCenter a where a.deptUid = :deptUid";
		
		try {
			this.pagingQuery(page, queryString, countQueryString, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}	
	
	
	
	
//	/**
//	 * @see the method use for ScheduelManager
//	 * @param deptUid
//	 * @param page
//	 * @return
//	 * @throws Exception
//	 */
//	public List<WorkCenter> getWorkCentersByDeptUid(String deptUid, Page<WorkCenter> page)
//			throws Exception {
//		List<WorkCenter> list = null;
//		try {
//			DetachedCriteria crit = DetachedCriteria.forClass(WorkCenter.class);
//			crit.add(Restrictions.like("deptUid", deptUid,MatchMode.START));
//
//			// 如果需要进行分页处理，则调用如下语句
//			if (page != null)
//				//author lcs Temporary annotation
//				this.pagingQuery(page, crit);
//
//			crit.addOrder(Property.forName("workCenterName").asc());
//
//			list = this.query(crit, null);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return list;
//	}
//
//	/**
//	 * @see the method use for ScheduleManager
//	 * @param deptUid
//	 * @param criticalLevel
//	 * @param page
//	 * @return
//	 * @throws Exception
//	 */
//	public List<WorkCenter> getWorkCentersByDeptUid(String deptUid,
//			Integer criticalLevel, Page<WorkCenter> page) throws Exception {
//		List<WorkCenter> list = null;
//		try {
//			Session session = this.getSession();
//			Criteria crit = session.createCriteria(WorkCenter.class);
//			crit.add(Restrictions.like("deptUid", deptUid,MatchMode.START));
//			if(criticalLevel!=null)
//				crit.add(Restrictions.lt("criticalLevel", criticalLevel));
//			// 如果需要进行分页处理，则调用如下语句
//			if (page != null)
//				//author lcs Temporary annotation
//				//page.pagePro(crit);
//			crit.addOrder(Property.forName("criticalLevel").asc());
//			crit.addOrder(Property.forName("workCenterName").asc());
//
//			list = crit.list();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return list;
//	}
// 
//	/**
//	 * @see the method use for SchedueleOperationTaskManager
//	 * @return
//	 * @throws Exception
//	 */
//	public List<WorkCenter> getCoWorkCenters() throws Exception {
//		List<WorkCenter> list = null;
//		try {
//			Session session = this.getSession();
//			Criteria crit = session.createCriteria(WorkCenter.class);
//			crit.add(Restrictions.like("workCenterName", "%协作%"));
//			crit.addOrder(Property.forName("workCenterName").asc());
//			list = crit.list();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return list;
//	}
	
	
	
}
